<template>
	<view class="container">
		<scroll-view scroll-y class="content" @scrolltolower="scrollBottom">
			<view class="content">
				<view class="nav flex">
					<view class="nav_item flex_center" :class="current==1?'active_nav':''" @tap="changeNav(1)">开仓</view>
					<view class="nav_item flex_center" :class="current==2?'active_nav':''" @tap="changeNav(0)">待处理
					</view>
					<view class="nav_bg" :class="current==1?'nav_left':'nav_right'"></view>
				</view>
				<view class="balance col_center">
					<view class="balance_title">
						{{current==1?'盈亏':'预期盈亏'}}
					</view>
					<view class="balance_detail" :class="yk > 0?'up':'down'">
						{{yk}}
					</view>
				</view>
				<!-- 			<view class="card">
				<view class="card_item flex_between">
					<view class="title">净值</view>
					<view class="detail">48807.08</view>
				</view>
				<view class="card_item flex_between">
					<view class="title">预付款比</view>
					<view class="detail">0%</view>
				</view>
				<view class="card_item flex_between">
					<view class="title">可用预付款</view>
					<view class="detail">18807.08</view>
				</view>
			</view> -->
				<view class="list_box">
					<view class="list" v-if="1==1">
						<view class="item" v-for="item in list" @tap="openPopup(item.id)">
							<view class="header flex_between">
								<view class="symbol font_title">{{item.pro_name}}</view>
								<view class="status">{{item.limit_type_str}}</view>
							</view>
							<view class="price flex_between">
								<view class="price_change flex">
									<text class="old_price">{{item.price}}</text>
									<u-icon name="arrow-rightward" size="12" style="margin: 0 10rpx;" color="#000"
										bold></u-icon>
									<text class="new_price"
										:class="item.type_str == '买入' ?'up':'down'">{{item.saleprice}}</text>
								</view>
								<view class="amount flex" :class="item.type_str == '买入'?'border_up':'border_down'">
									<view class="amount_title" :class="item.type_str == '买入'?'bg_up':'bg_down'">
										{{item.type_str}}
									</view>
									<view class="amount_detail" :class="item.type_str == '买入'?'up':'down'">{{item.num}}
										手数
									</view>
								</view>
							</view>
							<view class="detail flex_between">
								<view class="detail_list">
									<view class="detail_item">保证金 {{item.credit}}</view>
									<view class="detail_item">订单ID {{item.order_sn}}</view>
									<view class="detail_item">{{item.createtime}}</view>
								</view>
								<view class="detail_price" :class="item.type_str == '买入' ?'up':'down'">
									{{item.money}}
								</view>
							</view>
						</view>
					</view>
					<view class="no_data flex_center" v-else>
						<image src="../../static/img/empty.png"
							style="width: 420rpx;height:380rpx;margin-top: 30rpx;" />
					</view>
				</view>
			</view>
		</scroll-view>
		<u-popup :show="detailShow" @close="detailClose">
			<view class="mask">
				<view class="mask_close flex_center" @tap="detailClose">
					<uni-icons type="closeempty"></uni-icons>
				</view>
				<view class="mask_title font_title">{{"订单详情"}}</view>
				<view class="mask_content font_title">
					<view class="header flex_between">
						<view class="symbol">{{form.pro_name}}</view>
						<view class="status">{{form.limit_type_str}}</view>
					</view>
					<view class="price flex_between">
						<view class="price_change flex">
							<text class="old_price">{{form.price}}</text>
							<u-icon name="arrow-rightward" size="12" style="margin: 0 10rpx;" color="#000"
								bold></u-icon>
							<text class="new_price"
								:class="form.type_str == '买入' ?'up':'down'">{{form.saleprice}}</text>
						</view>
						<view class="amount flex" :class="form.type_str == '买入'?'border_up':'border_down'">
							<view class="amount_title" :class="form.type_str == '买入' ? 'bg_up' : 'bg_down'">
								{{form.type_str}}
							</view>
							<view class="amount_detail" :class="form.type_str == '买入'?'up':'down'">{{form.num}} 手数
							</view>
						</view>
					</view>
					<view class="detail flex_between">
						<view class="detail_item">
							<view class="item_title">止损</view>
							<view class="item_data">{{form.s_l}}</view>
						</view>
						<view class="detail_item">
							<view class="item_title">止盈</view>
							<view class="item_data">{{form.t_p}}</view>
						</view>
						<view class="detail_item">
							<view class="item_title">盈亏</view>
							<view class="item_data">{{form.money}}</view>
						</view>
						<view class="detail_item">
							<view class="item_title">手续费</view>
							<view class="item_data">{{form.commission}}</view>
						</view>
					</view>
					<view class="info flex_between">
						<view class="info_list">
							<view class="info_item">保证金 {{form.credit}}</view>
							<view class="info_item">订单ID {{form.order_sn}}</view>
							<view class="info_item">{{form.createtime}}</view>
						</view>
						<view class="info_price" :class="form.type_str == '买入' ?'up':'down'">
							{{form.sj_money}}
						</view>
					</view>
				</view>
				<view class="fixed" v-if="current == '1'">
					<view class="done btn flex_between" @tap="saleOrder">
						<text>平仓</text>
						<uni-icons custom-prefix="iconfont" type="icon-duigou" color="#fff" size="24"></uni-icons>
					</view>
				</view>
			</view>
		</u-popup>
		<tabbarView id="2"></tabbarView>
	</view>
</template>

<script>
	import tabbarView from "@/components/tabbar.vue"
	import {
		stockOrderListApi,
		orderDetailApi,
		saleOrderApi,
		yingkuiApi
	} from '@/api/api.js'
	export default {
		components: {
			tabbarView
		},
		data() {
			return {
				page: 1,
				form: {},
				current: 1,
				list: [],
				detailShow: false,
				yk: '',
				timer: null,
			};
		},
		onShow() {
			this.getStockOrder()
			this.yingkui()
			this.timer = setInterval(() => {
				this.yingkui()
				this.getStockOrder()
			}, 3000 * 10)
		},
		onHide() {
			clearInterval(this.timer)
		},
		onUnload() {
			clearInterval(this.timer)
		},
		beforeDestroy() {
			clearInterval(this.timer)
		},
		methods: {
			scrollBottom() {
				console.log("滚动到底部")
				this.page++
				this.getStockOrder()
			},
			// 获取盈亏
			yingkui() {
				const token = uni.getStorageSync('token')
				const status = this.current
				yingkuiApi({
					token,
					status
				}).then(res => {
					console.log(res.data.data)
					this.yk = res.data.data.yingkui
				})
			},
			// 平仓
			saleOrder() {
				const token = uni.getStorageSync('token')
				const id = this.form.id
				saleOrderApi({
					token,
					id
				}).then(res => {
					console.log(res.data.msg)
					uni.showToast({
						title: res.data.msg,
						icon: 'success',
						duration: 2000,
					});
					uni.reLaunch({
						url: '/pages/position/index'
					});
				})
			},
			// 获取订单详情
			getOrderDetail(options) {
				const token = uni.getStorageSync('token')
				const id = options
				orderDetailApi({
					token,
					id
				}).then(res => {
					this.form = res.data.data
				})
				this.timer = setInterval(() => {
					orderDetailApi({
						token,
						id
					}).then(res => {
						this.form = res.data.data
					})
				}, 3000 * 10)

			},
			// 获取订单
			getStockOrder() {
				const token = uni.getStorageSync('token')
				const status = this.current
				const page = this.page
				stockOrderListApi({
					page,
					token,
					status
				}).then(res => {
					if (this.page == 1) {
						this.list = res.data.data.data
					} else {
						this.list = [...this.list, ...res.data.data.data]
					}
				})
			},
			// 订单详情
			openPopup(opt) {
				this.detailShow = true
				console.log(opt)
				this.getOrderDetail(opt)
			},
			detailClose() {
				this.detailShow = false
			},
			changeNav(key) {
				if (key == this.current) return
				this.current = key
				console.log(this.current)
				this.getStockOrder()
				this.yingkui()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.content {
			.nav {
				height: 100rpx;
				line-height: 100rpx;
				background-color: rgba(237, 237, 237, .46);
				border-radius: 40rpx;
				overflow: hidden;
				position: relative;
				font-size: 32rpx;

				.nav_item {
					flex: 1;
					position: relative;
					z-index: 10;
					opacity: .3;
					transition: all .3s ease;
				}

				.nav_bg {
					position: absolute;
					top: 6rpx;
					background-image: url("../../static/img/page_bg.png");
					background-size: cover;
					z-index: 1;
					border-radius: 40rpx;
					width: calc(50% - 6rpx);
					height: calc(100% - 12rpx);
					z-index: 9;
					transition: all .3s ease;
				}

				.nav_left {
					left: 6rpx;
				}

				.nav_right {
					left: 50%;
				}

				.active_nav {
					color: #0065ff;
					opacity: 1;
				}
			}

			.balance {
				margin-top: 40rpx;

				.balance_title {
					opacity: .5;
					font-size: 28rpx;
				}

				.balance_detail {
					font-size: 70rpx;
					margin-top: 10rpx;
					font-family: PM;
				}
			}

			.card {
				box-shadow: 0 6rpx 16rpx 0 rgba(0, 0, 0, .16);
				border-radius: 30rpx;
				padding: 30rpx;
				margin-top: 30rpx;

				.card_item {
					margin-top: 10rpx;

					.title {
						opacity: .5;
						font-size: 24rpx;
					}

					.detail {
						font-size: 32rpx;
						font-family: PM;
					}

					&:nth-child(1) {
						margin-top: 0;
					}
				}
			}

			.list_box {
				.list {
					margin-top: 40rpx;

					.item {
						border-bottom: 2rpx solid rgba(237, 237, 237, 0.46);
						padding: 30rpx 0;

						.header {
							.symbol {
								font-size: 32rpx;
							}

							.status {
								font-size: 24rpx;
								opacity: .5;
							}
						}

						.price {
							.amount {
								font-size: 20rpx;
								border-radius: 10rpx;
								margin-top: 6rpx;

								.amount_title {
									padding: 4rpx 14rpx;
								}

								.amount_detail {
									padding: 4rpx 10rpx;
								}
							}
						}

						.detail {
							margin-top: 20rpx;

							.detail_list {
								opacity: .5;
								font-size: 20rpx;
							}

							.detail_price {
								font-size: 40rpx;
							}
						}
					}
				}
			}
		}

		.mask {
			padding: 40rpx 0;
			background: #fff;
			border-radius: 40rpx 40rpx 0px 0px;
			overflow: hidden;
			transition: all 0.25s linear 0s;
			position: relative;

			.mask_close {
				width: 72rpx;
				height: 72rpx;
				background-color: hsla(0, 0%, 92.9%, .46);
				border-radius: 20rpx;
				margin: 0 40rpx;
			}

			.mask_title {
				font-size: 50rpx;
				padding: 40rpx 40rpx 0;
			}

			.mask_content {
				margin-top: 40rpx;
				padding: 0 40rpx;

				.header {
					.symbol {
						font-size: 36rpx;
					}

					.status {
						font-size: 28rpx;
						opacity: .5;
					}
				}

				.price {
					.amount {
						font-size: 20rpx;
						border-radius: 10rpx;
						margin-top: 6rpx;

						.amount_title {
							padding: 4rpx 14rpx;
						}

						.amount_detail {
							padding: 4rpx 10rpx;
						}
					}
				}

				.detail {
					box-shadow: 0px 3px 8px 0 rgba(0, 0, 0, .16);
					text-align: center;
					margin-top: 30rpx;
					border-radius: 20rpx;
					padding: 20rpx 0;

					.detail_item {
						flex: 1;

						.item_title {
							opacity: .5;
							font-size: 24rpx;
						}

						.item_data {
							font-size: 32rpx;
							margin-top: 10rpx;
						}
					}
				}

				.info {
					margin-top: 30rpx;

					.info_list {
						opacity: .5;
						font-size: 24rpx;
						line-height: 34rpx;
					}

					.info_price {
						font-size: 40rpx;
					}

				}
			}

			.fixed {

				.btn {
					height: 100rpx;
					line-height: 100rpx;
					color: #fff;
					font-size: 32rpx;
					padding: 0 40rpx;
					border-radius: 20rpx;
					background-image: url("../../static/img/btn_bg.png");
					background-size: cover;
					background-repeat: no-repeat;
					overflow: hidden;
				}

				.done {
					background-color: #0065ff;
					margin-left: 80rpx;
					margin-top: 50rpx;
					border-radius: 20rpx 0 0 20rpx;
				}
			}
		}
	}
</style>